package com.goldcn.user.service;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.dao.RoleFunctionMapper;
import com.goldcn.user.dao.RoleMapper;
import com.goldcn.user.dao.UserRoleMapper;
import com.goldcn.user.model.Role;
import com.goldcn.user.model.RoleFunction;
import com.goldcn.user.model.UserRole;
import com.goldcn.user.vo.RoleAndFuncDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 1111 on 2016/11/8.
 */
@Service
@Transactional
public class RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleFunctionMapper roleFunctionMapper;

    //添加角色
    public Object addRole(Role role){
        Role re = roleMapper.selectByName(role.getRoleName(),SystemContext.getCompanyID());
        if(re != null){
            throw new ServiceException("该角色已经存在:"+role.getRoleName(), RestConst.ErrorCode.FORM_FAIL);
        }
        role.setRoleID(IdGen.get().nextId());
        role.setIsStop(0);
        role.setAddTime(CommonUtils.getCurTimestamp());
        role.setCompanyID(SystemContext.getCompanyID());
        roleMapper.insert(role);
        return role.getRoleID();
    }

    //角色名判断是否存在
    public Object judgeRoleName(String roleName){
        Role role = roleMapper.selectByName(roleName,SystemContext.getCompanyID());
        if(role==null){
            return 1;
        }else {
            return 0;
        }
    }

    //修改角色
    public String updateRole(Role role){
        if(role.getCode()==0){
            throw new ServiceException("默认角色不能修改",RestConst.ErrorCode.FORM_FAIL);
        }
        Role re = roleMapper.selectByName(role.getRoleName(),SystemContext.getCompanyID());
        if(re != null){
            throw new ServiceException("该角色已经存在:"+role.getRoleName(), RestConst.ErrorCode.FORM_FAIL);
        }
        roleMapper.updateByPrimaryKeySelective(role);
        return "success";
    }

    //用角色ID查询角色
    public Role selectByRoleID(Long roleID){
        return roleMapper.selectByPrimaryKey(roleID);
    }

    //查询所有角色列表
    public List<RoleAndFuncDto> roleList(){
        return roleMapper.selectRoleList(SystemContext.getCompanyID());
    }

    //查询角色及其功能权限
    public RoleAndFuncDto selectRoleAndFuncByID(Long roleID){
        return roleMapper.selectRoleAndFuncByID(roleID);
    }

    //删除角色
    public String deleteRole(Long roleID){
        Role role = roleMapper.selectByPrimaryKey(roleID);
        if(role.getCode()==0){
            throw new ServiceException("默认角色不能删除",RestConst.ErrorCode.FORM_FAIL);
        }
        List<UserRole> urList = userRoleMapper.selectByRoleID(roleID);
        if(urList.size()>0){
            throw new ServiceException("该角色已存在用户，不能删除",RestConst.ErrorCode.FORM_FAIL);
        }
        roleMapper.deleteByPrimaryKey(roleID);
        roleFunctionMapper.deleteByRoleID(roleID);
        return "success";
    }

    //查询角色的功能权限
    public List<RoleFunction> selectFunctionByRoleID(Long roleID){
        return roleFunctionMapper.selectByRoleID(roleID);
    }

    //更新角色的功能权限

    public List<RoleFunction> updateRoleFunction(Long roleID,List<RoleFunction>rfList){
        roleFunctionMapper.deleteByRoleID(roleID);
        for(RoleFunction rf : rfList){
            rf.setID(IdGen.get().nextId());
        }
        roleFunctionMapper.insertBatch(rfList);
        return roleFunctionMapper.selectByRoleID(roleID);
    }

    public List<Role> selAdmin(Long companyID,Integer code){
        return roleMapper.selAdmin(companyID,code);
    }

}
